package com.zz.jdk8.algo;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author zhr
 */
public class Fib1 {
    public static void main(String[] args) {

        long pre1 = 0;
        long pre2 = 0;
        long fib = 0;
        for(long i = 1; i < 100; i++) {
            if(i == 1 || i == 2) {
                fib = 1;
            } else {
                fib = pre1 + pre2;
            }
            pre1 = pre2;
            pre2 = fib;
            System.out.print(fib + " ");
            if(i > 2) {
                System.out.println("rate: " + pre2 * 1.0 / pre1);
            }
            if(i % 10 == 0) {
                System.out.println();
            }
            if(fib + pre2 < 0) {
                System.out.println();
                System.out.println("i = " + i);
                System.out.println(" > Long.MAX_VALUE");
                break;
            }
        }
        System.out.println();
        System.out.println("end");
    }

    private static long fib(long n) {
        if(n < 1) {
            return 0;
        } else if(n == 1 || n == 2) {
            return 1;
        }
        return fib(n - 2) + fib(n - 1);
    }
}
